<?php

namespace App\Imports;

use App\Http\Util\Tool;
use App\Models\Message;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;

class UsersImport implements ToCollection
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        try{
            // TODO: Implement collection() method.
            //解析excel中的数据
            $data=json_decode(json_encode($rows),true);
            //去掉第一行的标题
            array_splice($data, 0, 1);
            $log = '';
            //拼接字段入库
            foreach ($data as $row)
            {
//                $query['id']=$row[0];
                $query['phone']=$row[1];
                $query['name']=$row[2];
                $query['email']=$row[3];
                $query['content']=$row[4];
                if(!$result=Message::create($query)){
                    Tool::logInfo('数据导入失败', json_encode($query), $file = 'lumen');
                }
            }
        }catch (\Exception $e){
                    Tool::logError('数据导入失败',$e->getMessage(), $file = 'lumen');
        }
    }


    public function createData($rows)
    {
        //todo
    }
}
